package com.ruoyi.project.common.appchannel.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.common.appchannel.domain.AppChannel;
import com.ruoyi.project.common.appchannel.service.IAppChannelService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 应用渠道Controller
 *
 * @author shixiangwen
 * @date 2022-03-21
 */
@Controller
@RequestMapping("/common/appchannel")
public class AppChannelController extends BaseController {

    private String prefix = "common/appchannel";

    @Autowired
    private IAppChannelService appChannelService;

    @RequiresPermissions("common:appchannel:view")
    @GetMapping()
    public String appchannel() {
        return prefix + "/appchannel";
    }

    /**
     * 查询应用渠道列表
     */
    @RequiresPermissions("common:appchannel:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AppChannel appChannel) {
        startPage();
        List<AppChannel> list = appChannelService.selectAppChannelList(appChannel);
        return getDataTable(list);
    }

    /**
     * 导出应用渠道列表
     */
    @RequiresPermissions("common:appchannel:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AppChannel appChannel) {
        List<AppChannel> list = appChannelService.selectAppChannelList(appChannel);
        ExcelUtil<AppChannel> util = new ExcelUtil<AppChannel>(AppChannel.class);
        return util.exportExcel(list, "appchannel");
    }

    /**
     * 新增应用渠道
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存应用渠道
     */
    @RequiresPermissions("common:appchannel:add")
    @Log(title = "应用渠道", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AppChannel appChannel) {
        return toAjax(appChannelService.insertAppChannel(appChannel));
    }

    /**
     * 修改应用渠道
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        AppChannel appChannel = appChannelService.selectAppChannelById(id);
        mmap.put("appChannel", appChannel);
        return prefix + "/edit";
    }

    /**
     * 修改保存应用渠道
     */
    @RequiresPermissions("common:appchannel:edit")
    @Log(title = "应用渠道", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AppChannel appChannel) {
        return toAjax(appChannelService.updateAppChannel(appChannel));
    }

    /**
     * 删除应用渠道
     */
    @RequiresPermissions("common:appchannel:remove")
    @Log(title = "应用渠道", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(appChannelService.deleteAppChannelByIds(ids));
    }
}
